// DlgHistoryList.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgHistoryList.h"
#include "DlgEditRequest.h"

// CDlgHistoryList dialog

IMPLEMENT_DYNAMIC(CDlgHistoryList, CDialog)

CDlgHistoryList::CDlgHistoryList(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgHistoryList::IDD, pParent)
	, m_szFind(_T(""))
{
	m_nID = -1;
	m_nStaffID = -1;
	m_pszaStatus = NULL;
	m_paStaffInfo = NULL;
	m_nType = HISTORY_REQUEST;
	m_bSortAscending = TRUE;
	m_nCurrentSort = 0;
	m_nCurrentRecord = -1;
}

CDlgHistoryList::~CDlgHistoryList()
{
}

void CDlgHistoryList::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_HISTORY, m_ctrlHistory);
	DDX_Text(pDX, IDC_EDIT_FIND, m_szFind);
	DDX_Control(pDX, IDC_DATETIMEPICKER_FIRST, m_ctlDateFirst);
}


BEGIN_MESSAGE_MAP(CDlgHistoryList, CDialog)
	ON_NOTIFY(NM_RCLICK, IDC_LIST_HISTORY, &CDlgHistoryList::OnNMRClickListHistory)
	ON_NOTIFY(HDN_ITEMCLICK, 0, &CDlgHistoryList::OnHdnItemclickListHistory)
	ON_BN_CLICKED(IDC_BUTTON_FIND, &CDlgHistoryList::OnBnClickedButtonFind)
	ON_BN_CLICKED(IDC_BUTTON_ALL, &CDlgHistoryList::OnBnClickedButtonAll)
	ON_EN_CHANGE(IDC_EDIT_FIND, &CDlgHistoryList::OnEnChangeEditFind)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_HISTORY, &CDlgHistoryList::OnNMDblclkListHistory)
	ON_NOTIFY(LVN_ITEMCHANGING, IDC_LIST_HISTORY, &CDlgHistoryList::OnLvnItemchangingListHistory)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER_FIRST, &CDlgHistoryList::OnDtnDatetimechangeDatetimepickerFirst)
END_MESSAGE_MAP()


// CDlgHistoryList message handlers

BOOL CDlgHistoryList::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nStaffID > 0 );

	// set extended styles
#ifdef _UNICODE
	m_ctrlHistory.SetView( LVS_REPORT );
#endif
	m_ctrlHistory.SetExtendedStyle( LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_SUBITEMIMAGES | LVS_EX_INFOTIP  );

	// insert columns
	m_ctrlHistory.InsertColumn( HST_DATE, _T("Date"), LVCFMT_LEFT,  100, HST_DATE );
	m_ctrlHistory.InsertColumn( HST_REQUEST_ID, _T("Item ID"), LVCFMT_LEFT, 85, HST_REQUEST_ID );
	m_ctrlHistory.InsertColumn( HST_USER_EMAIL, _T("Email"), LVCFMT_LEFT, 70, HST_USER_EMAIL );
	m_ctrlHistory.InsertColumn( HST_STAFF_ID, _T("Staff"), LVCFMT_LEFT, 70, HST_STAFF_ID );
	// insert columns for list type
	if( m_nType == HISTORY_EXTERNAL_ILL )
		m_ctrlHistory.InsertColumn( HST_STATUS, _T("Link"), LVCFMT_LEFT, 120, HST_STATUS );
	else
		m_ctrlHistory.InsertColumn( HST_STATUS, _T("Status"), LVCFMT_LEFT, 120, HST_STATUS );
	m_ctrlHistory.InsertColumn( HST_DESCRIPTION, _T("Description"), LVCFMT_LEFT, 120, HST_DESCRIPTION );

	// show history for type
	if( m_nType == HISTORY_REQUEST )
	{
		ASSERT( m_pszaStatus != NULL );
		ASSERT( m_paStaffInfo != NULL );

		// default sort
		m_szSort = _T("date_in");
		// set filter
		m_szFilter.Format( _T("request_id = %ld"), m_nID );

		// set title
		CString szTitle;
		szTitle.Format( _T("History for request: %.8ld"), m_nID );
		SetWindowText( szTitle );

		// hide date
		GetDlgItem( IDC_DATETIMEPICKER_FIRST )->ShowWindow( SW_HIDE );
	}
	else if( m_nType == HISTORY_EXTERNAL_ILL )
	{
		ASSERT( m_paStaffInfo != NULL );

		// default sort
		m_szSort = _T("date_out DESC");

		if( m_nID > 0 )
		{
			// set filter
			m_szFilter.Format( _T("user_id = %ld"), m_nID );

			// set title
			CString szTitle;
			szTitle.Format( _T("History for user: %.8ld"), m_nID );
			SetWindowText( szTitle );

			// hide date
			GetDlgItem( IDC_DATETIMEPICKER_FIRST )->ShowWindow( SW_HIDE );
		}
		else if( !m_szFind.IsEmpty() )
		{
			CString szFilter;

			// set main filter
			m_szFilter.Format( _T("(description LIKE '%%%s%%' OR link LIKE '%%%s%%')"), m_szFind, m_szFind );

			// get staff
			CArray<STAFF_SET> aStaff;
			szFilter.Format( _T("(staff.name LIKE '%%%s%%' OR staff.email LIKE '%%%s%%' OR staff.phone LIKE '%%%s%%') "), m_szFind, m_szFind, m_szFind );
			appConfig.m_SQL.GetStaffSet( &aStaff, szFilter );
			// check for user
			if( aStaff.GetCount() > 0 )
			{
				CString szTemp;
				CString szStaff;
				if( !m_szFilter.IsEmpty() )
					m_szFilter += _T(" OR ");
				for( INT nIndex = 0; nIndex < aStaff.GetCount(); nIndex++ )
				{
					if( !szStaff.IsEmpty() )
						szTemp.Format( _T(" OR staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					else
						szTemp.Format( _T("(staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					szStaff += szTemp;
				}
				szStaff += _T(")");
				m_szFilter += szStaff;
			}

			// get ILL users
			CArray<EXTERNAL_ILL_SET> aILL;
			szFilter.Format( _T("(external_ill.email LIKE '%%%s%%' OR external_ill.company LIKE '%%%s%%') "), m_szFind, m_szFind );
			appConfig.m_SQL.GetExternIllSet( &aILL, szFilter );
			// check for user
			if( aILL.GetCount() > 0 )
			{
				CString szTemp;
				CString szUser;
				if( !m_szFilter.IsEmpty() )
					m_szFilter += _T(" OR ");
				for( INT nIndex = 0; nIndex < aILL.GetCount(); nIndex++ )
				{
					if( !szUser.IsEmpty() )
						szTemp.Format( _T(" OR user_id=%ld"), aILL.GetAt( nIndex ).m_id );
					else
						szTemp.Format( _T("(user_id=%ld"), aILL.GetAt( nIndex ).m_id );
					szUser += szTemp;
				}
				szUser += _T(")");
				m_szFilter += szUser;
			}

			// set title
			CString szTitle;
			szTitle.Format( _T("Find in External ILL History: %s"), m_szFind );
			SetWindowText( szTitle );

			// hide date and all button
			GetDlgItem( IDC_DATETIMEPICKER_FIRST )->ShowWindow( SW_HIDE );
			GetDlgItem( IDC_BUTTON_ALL )->ShowWindow( SW_HIDE );
			GetDlgItem( IDC_EDIT_FIND )->ShowWindow( SW_HIDE );
			GetDlgItem( IDC_BUTTON_FIND )->ShowWindow( SW_HIDE );			
		}
		else
		{
			// set filter date in
			CTime time(CTime::GetCurrentTime());
			m_szFilter.Format( _T("external_ill_history.date_out >= '%s' AND external_ill_history.date_out <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
		}
	}
	else if( m_nType == HISTORY_RECENT )
	{
		ASSERT( m_pszaStatus != NULL );
		ASSERT( m_paStaffInfo != NULL );

		// default sort
		m_szSort = _T("date_in DESC");
		m_bSortAscending = FALSE;
		// set filter date in
		CTime time(CTime::GetCurrentTime());
		m_szFilter.Format( _T("date_in >= '%s' AND date_in <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));

		// set title
		CString szTitle;
		szTitle.Format( _T("History: %s"), time.Format( appConfig.m_General.szShortDateFormat ));
		SetWindowText( szTitle );
	}
	else
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// fill list
	FillList();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgHistoryList::OnBnClickedButtonAll()
{
	// clear search
	if( m_nType == HISTORY_REQUEST )
	{
		// set filter
		m_szFilter.Format( _T("request_id = %ld"), m_nID );
	}
	else if( m_nType == HISTORY_EXTERNAL_ILL )
	{
		if( m_nID > 0 )
		{
			// set filter
			m_szFilter.Format( _T("user_id = %ld"), m_nID );

			// set title
			CString szTitle;
			szTitle.Format( _T("History for user: %.8ld"), m_nID );
			SetWindowText( szTitle );
		}
		else
		{
			// set filter date in
			CTime time(CTime::GetCurrentTime());
			m_szFilter.Format( _T("external_ill_history.date_out >= '%s' AND external_ill_history.date_out <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
		}
	}
	else if( m_nType == HISTORY_RECENT )
	{
		// set date in
		CTime time(CTime::GetCurrentTime());
		m_szFilter.Format( _T("date_in >= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ));
	}
	// fill list
	FillList();
}

void CDlgHistoryList::OnBnClickedButtonFind()
{
	// update data
	UpdateData();

	// build search
	if( m_nType == HISTORY_REQUEST )
	{
		// set main filter
		m_szFilter.Format( _T("request_id = %ld AND (description LIKE '%%%s%%' "), m_nID, m_szFind );

		// get list of staff
		if( !m_szFilter.IsEmpty() )
		{
			CArray<STAFF_SET> aStaff;
			CString szFilter;
			// get users
			szFilter.Format( _T("(staff.name LIKE '%%%s%%' OR staff.email LIKE '%%%s%%' OR staff.phone LIKE '%%%s%%') "), m_szFind, m_szFind, m_szFind );
			appConfig.m_SQL.GetStaffSet( &aStaff, szFilter );
			// check for user
			if( aStaff.GetCount() > 0 )
			{
				CString szTemp;
				CString szStaff;
				if( !m_szFilter.IsEmpty() )
					m_szFilter += _T(" OR ");
				for( INT nIndex = 0; nIndex < aStaff.GetCount(); nIndex++ )
				{
					if( !szStaff.IsEmpty() )
						szTemp.Format( _T(" OR user_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					else
						szTemp.Format( _T("(user_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					szStaff += szTemp;
				}
				szStaff += _T(")");
				m_szFilter += szStaff;
			}
		}
		// add ending
		m_szFilter += _T(") ");
	}
	else if( m_nType == HISTORY_EXTERNAL_ILL )
	{
		CString szTemp;
		// set main filter
		if( m_nID > 0 )
			m_szFilter.Format( _T("user_id = %ld AND ((description LIKE '%%%s%%' OR link LIKE '%%%s%%') "), m_nID, m_szFind, m_szFind );
		else
		{
			// get date start
			CTime time;
			if( m_ctlDateFirst.GetTime( time ) != GDT_NONE )
				szTemp.Format( _T("external_ill_history.date_out >= '%s' AND external_ill_history.date_out <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
			else
			{
				time = CTime::GetCurrentTime();
				szTemp.Format( _T("external_ill_history.date_out >= '%s' AND external_ill_history.date_out <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
				// set window title
				SetWindowText( _T("History: Today") );
			}

			// set main filter
			m_szFilter.Format( _T("%s AND ((description LIKE '%%%s%%' OR link LIKE '%%%s%%') "), szTemp, m_szFind, m_szFind );
		}

		// get list of staff
		if( !m_szFilter.IsEmpty() )
		{
			CArray<STAFF_SET> aStaff;
			CString szFilter;
			// get users
			szFilter.Format( _T("(staff.name LIKE '%%%s%%' OR staff.email LIKE '%%%s%%' OR staff.phone LIKE '%%%s%%') "), m_szFind, m_szFind, m_szFind );
			appConfig.m_SQL.GetStaffSet( &aStaff, szFilter );
			// check for user
			if( aStaff.GetCount() > 0 )
			{
				CString szStaff;
				if( !m_szFilter.IsEmpty() )
					m_szFilter += _T(" OR ");
				for( INT nIndex = 0; nIndex < aStaff.GetCount(); nIndex++ )
				{
					if( !szStaff.IsEmpty() )
						szTemp.Format( _T(" OR staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					else
						szTemp.Format( _T("(staff_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					szStaff += szTemp;
				}
				szStaff += _T(")");
				m_szFilter += szStaff;
			}
		}
		// add ending
		m_szFilter += _T(") ");
	}
	else if( m_nType == HISTORY_RECENT )
	{
		CString szTemp;
		
		// get date start
		CTime time;
		if( m_ctlDateFirst.GetTime( time ) != GDT_NONE )
			szTemp.Format( _T("date_in >= '%s' AND date_in <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
		else
		{
			time = CTime::GetCurrentTime();
			szTemp.Format( _T("date_in >= '%s' AND date_in <= '%s' "), time.Format( _T("%Y-%m-%d 00:00:00") ), time.Format( _T("%Y-%m-%d 24:00:00") ));
			// set window title
			SetWindowText( _T("History: Today") );
		}

		// set main filter
		m_szFilter.Format( _T("%s AND ((request_id LIKE '%%%s%%' OR description LIKE '%%%s%%')"), szTemp, m_szFind, m_szFind );

		// get list of staff
		if( !m_szFilter.IsEmpty() )
		{
			CArray<STAFF_SET> aStaff;
			CString szFilter;
			// get users
			szFilter.Format( _T("(staff.name LIKE '%%%s%%' OR staff.email LIKE '%%%s%%' OR staff.phone LIKE '%%%s%%') "), m_szFind, m_szFind, m_szFind );
			appConfig.m_SQL.GetStaffSet( &aStaff, szFilter );
			// check for user
			if( aStaff.GetCount() > 0 )
			{
				CString szStaff;
				if( !m_szFilter.IsEmpty() )
					m_szFilter += _T(" OR ");
				for( INT nIndex = 0; nIndex < aStaff.GetCount(); nIndex++ )
				{
					if( !szStaff.IsEmpty() )
						szTemp.Format( _T(" OR user_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					else
						szTemp.Format( _T("(user_id=%ld"), aStaff.GetAt( nIndex ).m_id );
					szStaff += szTemp;
				}
				szStaff += _T(")");
				m_szFilter += szStaff;
			}
		}
		// add ending
		m_szFilter += _T(") ");
	}
	// fill list
	FillList();
}

void CDlgHistoryList::OnEnChangeEditFind()
{
	// set default button 
	SetDefaultButton( IDC_BUTTON_FIND );
}

void CDlgHistoryList::FillList()
{
	CWaitCursor wait;

	// clear list
	m_ctrlHistory.DeleteAllItems();

	// get type
	if( m_nType == HISTORY_REQUEST )
	{
		// get history
		CArray<REQUEST_HISTORY> aHistory;
		appConfig.m_SQL.GetRequestHistorys( &aHistory, m_szFilter, m_szSort );

		CString szName;
		INT nIndex = 0;
		INT nItem = 0;

		// loop through set
		for( LONG nIndex = 0; nIndex < aHistory.GetCount(); nIndex++ )
		{
			// get History
			REQUEST_HISTORY History;
			History = aHistory.GetAt( nIndex );
			// get name
			CString szName;
			if( History.m_user_id == -1 )
				szName = _T("User");
			else
				szName = m_paStaffInfo->GetAt( History.m_user_id ).szStaffName;
			// insert item
			nItem = m_ctrlHistory.InsertItem( nIndex, History.m_date_in.Format( appConfig.m_General.szDateFormat ) );
			// add text
			m_ctrlHistory.SetItemText( nItem, HST_REQUEST_ID, _T("") );
			m_ctrlHistory.SetItemText( nItem, HST_USER_EMAIL, _T("") );
			m_ctrlHistory.SetItemText( nItem, HST_STAFF_ID, szName );
			if( History.m_status_id < 0 )
				History.m_status_id = 0;
			m_ctrlHistory.SetItemText( nItem, HST_STATUS, m_pszaStatus->GetAt( History.m_status_id ) );
			m_ctrlHistory.SetItemText( nItem, HST_DESCRIPTION, History.m_description );
		}
	}
	else if( m_nType == HISTORY_EXTERNAL_ILL )
	{
		// get history
		CArray<EXTERNAL_ILL_HISTORY> aHistory;
		appConfig.m_SQL.GetExternIllHistorys( &aHistory, m_szFilter, m_szSort );

		CString szName;
		INT nIndex = 0;
		INT nItem = 0;

		// loop through set
		for( LONG nIndex = 0; nIndex < aHistory.GetCount(); nIndex++ )
		{
			// get History
			EXTERNAL_ILL_HISTORY History;
			History = aHistory.GetAt( nIndex );

			// get staff
			szName = m_paStaffInfo->GetAt( History.m_staff_id ).szStaffName;

			// insert item
			nItem = m_ctrlHistory.InsertItem( nIndex, History.m_date_out.Format( appConfig.m_General.szDateFormat ) );
			// add text
			m_ctrlHistory.SetItemText( nItem, HST_REQUEST_ID, _T("") );
			m_ctrlHistory.SetItemText( nItem, HST_USER_EMAIL, History.m_user_email );
			m_ctrlHistory.SetItemText( nItem, HST_STAFF_ID, szName );
			m_ctrlHistory.SetItemText( nItem, HST_STATUS, History.m_link );
			m_ctrlHistory.SetItemText( nItem, HST_DESCRIPTION, History.m_description );
		}
	}
	else if( m_nType == HISTORY_RECENT )
	{
		// get history
		CArray<REQUEST_HISTORY> aHistory;
		appConfig.m_SQL.GetRequestHistorys( &aHistory, m_szFilter, m_szSort );

		CString szName;
		INT nIndex = 0;
		INT nItem = 0;

		// loop through set
		for( LONG nIndex = 0; nIndex < aHistory.GetCount(); nIndex++ )
		{
			// get History
			REQUEST_HISTORY History;
			History = aHistory.GetAt( nIndex );
			// get name
			CString szName;
			if( History.m_user_id == -1 )
				szName = _T("User");
			else
				szName = m_paStaffInfo->GetAt( History.m_user_id ).szStaffName;
			// get request ID
			CString szRequestID;
			szRequestID.Format( _T("%.8ld"), History.m_request_id );

			// insert item
			nItem = m_ctrlHistory.InsertItem( nIndex, History.m_date_in.Format( appConfig.m_General.szDateFormat ) );
			// add text
			m_ctrlHistory.SetItemText( nItem, HST_REQUEST_ID, szRequestID );
			m_ctrlHistory.SetItemText( nItem, HST_USER_EMAIL, _T("") );
			m_ctrlHistory.SetItemText( nItem, HST_STAFF_ID, szName );
			if( History.m_status_id < 0 )
				History.m_status_id = 0;
			m_ctrlHistory.SetItemText( nItem, HST_STATUS, m_pszaStatus->GetAt( History.m_status_id ) );
			m_ctrlHistory.SetItemText( nItem, HST_DESCRIPTION, History.m_description );
		}
	}

	// set column widths
	if( m_ctrlHistory.GetItemCount() > 0 )
	{
		m_ctrlHistory.SetColumnWidth( HST_DATE, LVSCW_AUTOSIZE );
		m_ctrlHistory.SetColumnWidth( HST_REQUEST_ID, LVSCW_AUTOSIZE );
		m_ctrlHistory.SetColumnWidth( HST_USER_EMAIL, LVSCW_AUTOSIZE );
		m_ctrlHistory.SetColumnWidth( HST_STAFF_ID, LVSCW_AUTOSIZE );
		m_ctrlHistory.SetColumnWidth( HST_STATUS, LVSCW_AUTOSIZE );
		m_ctrlHistory.SetColumnWidth( HST_DESCRIPTION, LVSCW_AUTOSIZE );
	}

	// hide colums
	if( m_nType == HISTORY_RECENT )
	{
		m_ctrlHistory.SetColumnWidth( HST_USER_EMAIL, 0 );
	}
	else if( m_nType == HISTORY_EXTERNAL_ILL )
	{
		m_ctrlHistory.SetColumnWidth( HST_REQUEST_ID, 0 );
	}
	else
	{
		m_ctrlHistory.SetColumnWidth( HST_REQUEST_ID, 0 );
		m_ctrlHistory.SetColumnWidth( HST_USER_EMAIL, 0 );
	}
}

INT CDlgHistoryList::SetDefaultButton(const DWORD nNewId) 
{
	DWORD nPrevId;

	nPrevId  = GetDefID();
	// Its not automatic - we have to remove the border from the old button
	SendDlgItemMessage(nPrevId, BM_SETSTYLE, BS_PUSHBUTTON, (LPARAM)TRUE);

	SetDefID(nNewId);
	// Likewise, add a border to the new one
	SendDlgItemMessage(nNewId, BM_SETSTYLE, BS_DEFPUSHBUTTON, (LPARAM)TRUE);

	return nPrevId;
}

void CDlgHistoryList::OnNMRClickListHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

	LVHITTESTINFO lvhti;
	lvhti.pt = pNMLV->ptAction;

	//ask the list control where the cursor was when the right click happened
	if( m_ctrlHistory.SubItemHitTest( &lvhti ) != -1 )
	{
		// check for link
		if( m_nType == HISTORY_EXTERNAL_ILL && lvhti.iSubItem == HST_STATUS )
		{
			// get URL
			CString szURL = m_ctrlHistory.GetItemText( lvhti.iItem, lvhti.iSubItem );
			// check it
			if( !szURL.IsEmpty() )
			{
				if( AfxMessageBox( _T("Open Link in Web Browser?"), MB_YESNO ) == IDYES )
				{
					ShellExecute( NULL, _T("open"), szURL, NULL, NULL, SW_SHOWNORMAL );
				}
			}
		}
		// check for link
		if( (m_nType == HISTORY_REQUEST || m_nType == HISTORY_RECENT) && lvhti.iSubItem == HST_REQUEST_ID )
		{
			// get request ID
			CString szRequestID = m_ctrlHistory.GetItemText( lvhti.iItem, lvhti.iSubItem );
			// check it
			if( !szRequestID.IsEmpty() )
			{
				if( AfxMessageBox( _T("Edit this request?"), MB_YESNO ) == IDYES )
				{
					CDlgEditRequest dlgEditRequest;
					// set varialbes
					REQUEST_SET currentRequest;
					appConfig.m_SQL.GetRequestSet( &currentRequest, _tstol( szRequestID ) );

					dlgEditRequest.m_nStaffID = m_nStaffID;
					dlgEditRequest.m_nItemID = currentRequest.m_id;
					if( currentRequest.m_status_id >= STATUS_OFFSET_NA )
						dlgEditRequest.m_bComplete = TRUE;

					// show dialog
					if( dlgEditRequest.DoModal() == IDOK )
					{
						FillList();
					}
				}
			}
		}
		// handled
		*pResult = 1;
	}
}

void CDlgHistoryList::OnHdnItemclickListHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

	HDITEM hdi;
	CHeaderCtrl *pHeader = m_ctrlHistory.GetHeaderCtrl();
	ASSERT( pHeader );

	// toggle sort
	if( m_nCurrentSort == phdr->iItem )
		m_bSortAscending = !m_bSortAscending;
	else
	{
		// set sort type
		m_bSortAscending = TRUE;
		// set header information
		memset( &hdi, NULL, sizeof( HDITEM ));
		pHeader->GetItem( m_nCurrentSort, &hdi );
		hdi.mask |= HDI_FORMAT;
		hdi.fmt |= HDF_STRING;
		pHeader->SetItem( m_nCurrentSort, &hdi );
	}
	// set current sort
	m_nCurrentSort = phdr->iItem;

	// enum HISTORY{ HST_DATE, HST_REQUEST_ID, HST_STAFF_ID, HST_STATUS, HST_DESCRIPTION };

	// check for sort
	switch( m_nCurrentSort )
	{
	case HST_DATE:
		if( m_nType == HISTORY_REQUEST || m_nType == HISTORY_RECENT )
			m_szSort = _T("date_in");
		else if( m_nType == HISTORY_EXTERNAL_ILL )
			m_szSort = _T("date_out");
		else
			m_szSort = _T("");
		break;
	case HST_REQUEST_ID:
		if( m_nType == HISTORY_RECENT )
			m_szSort = _T("request_id");
		else
			m_szSort = _T("");
		break;
	case HST_USER_EMAIL:
		if( m_nType == HISTORY_EXTERNAL_ILL )
			m_szSort = _T("external_ill.email");
		else
			m_szSort = _T("");
		break;
	case HST_STAFF_ID:
		if( m_nType == HISTORY_EXTERNAL_ILL )
			m_szSort = _T("staff.name");
		else
			m_szSort = _T("user_id");
		break;
	case HST_STATUS:
		if( m_nType == HISTORY_EXTERNAL_ILL )
			m_szSort = _T("link");
		else
			m_szSort = _T("status_id");
		break;
	case HST_DESCRIPTION:
			m_szSort = _T("description");
			break;
	default:
		*pResult = 0;
		return;
	}

	// set sort type
	if( !m_szSort.IsEmpty() )
	{
		if( m_bSortAscending )
			m_szSort += _T(" ASC");
		else
			m_szSort += _T(" DESC");
	}

	// set header information
	if( m_nCurrentSort >= 0 )
	{
		memset( &hdi, NULL, sizeof( HDITEM ));
		pHeader->GetItem( m_nCurrentSort, &hdi );
		hdi.mask |= HDI_FORMAT;
		if( m_bSortAscending )
			hdi.fmt |= HDF_STRING | HDF_SORTUP;
		else
			hdi.fmt |= HDF_STRING | HDF_SORTDOWN;
		pHeader->SetItem( m_nCurrentSort, &hdi );
	}

	// fill list
	FillList();

	// retrun
	*pResult = 0;
}

void CDlgHistoryList::OnNMDblclkListHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

	LVHITTESTINFO lvhti;
	lvhti.pt = pNMItemActivate->ptAction;

	//ask the list control where the cursor was when the double click happened
	if( m_ctrlHistory.SubItemHitTest( &lvhti ) != -1 )
	{
		// check for link
		if( m_nType == HISTORY_RECENT )
		{
			// get request id
			CString szRequestID = m_ctrlHistory.GetItemText( lvhti.iItem, HST_REQUEST_ID );
			// check it
			if( !szRequestID.IsEmpty() )
			{
				if( AfxMessageBox( _T("Edit this request?"), MB_YESNO ) == IDYES )
				{
					CDlgEditRequest dlgEditRequest;
					// set varialbes
					REQUEST_SET currentRequest;
					appConfig.m_SQL.GetRequestSet( &currentRequest, _tstol( szRequestID ) );

					dlgEditRequest.m_nStaffID = m_nStaffID;
					dlgEditRequest.m_nItemID = currentRequest.m_id;
					if( currentRequest.m_status_id >= STATUS_OFFSET_NA )
						dlgEditRequest.m_bComplete = TRUE;

					// show dialog
					if( dlgEditRequest.DoModal() == IDOK )
					{
						FillList();
					}
				}
			}
		}
	}
	*pResult = 0;
}

void CDlgHistoryList::OnLvnItemchangingListHistory(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

	// get new selection
	if( pNMLV->uNewState & LVIS_SELECTED )
	{
		// set current record
		m_nCurrentRecord = pNMLV->iItem;
	}
	// result
	*pResult = 0;
}

void CDlgHistoryList::OnDtnDatetimechangeDatetimepickerFirst(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);

	// get date start
	CTime time(CTime::GetCurrentTime());
	if( m_ctlDateFirst.GetTime( time ) != GDT_NONE )
	{
		// set title
		CString szTitle;
		szTitle.Format( _T("History: %s"), time.Format( appConfig.m_General.szShortDateFormat ));
		SetWindowText( szTitle );
	}

	// update list via find
	OnBnClickedButtonFind();

	*pResult = 0;
}
